﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        private string maNV1;
        private byte cf = 0;

        private void FrmMain_Load(object sender, EventArgs e)
        {
            loadForm(false);
        }
        
        #region Các button xử lý login
        //Hàm load form
        void loadForm(bool k)
        {
            exp_AnPham.Enabled = k;
            exp_LuuThong.Enabled = k;
            exp_QLBanDoc.Enabled = k;
            ribbonBar_CSDL.Enabled = k;
            ribbonPanel_AnPham.Enabled = k;
            ribbonPanel_BanDoc.Enabled = k;
            ribbonPanel_DanhMuc.Enabled = k;
            ribbonPanel_LuuThong.Enabled = k;
            ribbonPanel_NSD.Enabled = k;
            ribbonPanel_ThongKe.Enabled = k;
            ribbonPanel_TimKiem.Enabled = k;
            btn_DoiMK.Enabled = k;
            btn_DoiMK_Top.Enabled = k;
            btn_Dangxuat.Enabled = k;
            btn_DangXuat_Top.Enabled = k;
            //btn_DangXuat_Top.Enabled = k;
            //
            lbl_status.Text = "Trạng thái chưa đăng nhập";
            lbl_TenNV.Text = "";
            lbl_timeNow.Text = "";
            lbl_TimeStart.Text = "00:00:00";
        }
        public void GetValue(string status, string manv, string tenNV, string timeStart)
        {
            maNV1 = manv;
            lbl_status.Text = status;
            lbl_TenNV.Text = tenNV;
            lbl_TimeStart.Text = timeStart;   
        }

        private void btn_DangNhap_Click(object sender, EventArgs e)
        {
            FrmLogin frm = new FrmLogin();
            frm.MyGetData = new FrmLogin.GetString(GetValue);
            frm.ShowDialog();            
            string[] arrayString = conn.getArray("MaCN", "QuyenHan", "MaNV", maNV1, 1);
            int n = arrayString.Length;//Lấy số phần tử mảng
            if (lbl_status.Text != "Trạng thái chưa đăng nhập")
            {
                
                //Duyệt các menu chuc nang
                foreach (Control c in ribbonControl_Menu.Controls)
                {
                    if (uti.TimKiem(arrayString, n, c.Name) == true)
                        c.Enabled = true;
                }
                foreach (Control c in ribbonPanel_HeThong.Controls)
                {
                    if (uti.TimKiem(arrayString, n, c.Name) == true)
                        c.Enabled = true;
                }
                foreach (Control c in ribbonBar_NSD.Controls)
                {
                    if (uti.TimKiem(arrayString, n, c.Name) == true)
                        c.Enabled = true;
                }
                //Load menu trái
                if (ribbonPanel_AnPham.Enabled == true)
                    exp_AnPham.Enabled = true;
                if (ribbonPanel_BanDoc.Enabled == true)
                    exp_QLBanDoc.Enabled = true;
                if (ribbonPanel_LuuThong.Enabled == true)
                    exp_LuuThong.Enabled = true;
                //Load trạng thái 2 button Dang nhap va dang xuất
                btn_DangNhap.Enabled = false;
                btn_DangNhap_Top.Enabled = false;
                btn_Dangxuat.Enabled = true;
                btn_DangXuat_Top.Enabled = true;
                btn_DoiMK.Enabled = true;
                btn_DoiMK_Top.Enabled = true;
                timer1_Tick(sender, e);
            }
        }

        private void btn_Dangxuat_Click(object sender, EventArgs e)
        {            
            loadForm(false);
            FrmMain_Load(sender, e);
            btn_DangNhap.Enabled = true;
            btn_DangNhap_Top.Enabled = true;
            //FrmLogin frm = new FrmLogin();
            //frm.ShowDialog();
        }

        private void btn_Thoát_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            lbl_timeNow.Text = System.DateTime.Now.ToLongTimeString();
        }
        #endregion
       
        #region QLNSD
        private void btn_DoiMK_Click(object sender, EventArgs e)
        {
            FrmDoiMK frm = new FrmDoiMK();
            frm.manv = maNV1;
            frm.ShowDialog();
        }
        private void btn_Backup_Click(object sender, EventArgs e)
        {
            FrmBackUp frm = new FrmBackUp();
            frm.ShowDialog();
        }

        private void btn_QTNSD_Click(object sender, EventArgs e)
        {
            Frm_QTNSD frm = new Frm_QTNSD();
            frm.ShowDialog();
        }

        #endregion
       
        #region Cap nhat DM
        private void btn_QLDanhMuc_Click(object sender, EventArgs e)
        {
            FrmDanhMuc frm = new FrmDanhMuc();
            frm.manv = maNV1;
            frm.ShowDialog();
        }
        private void btn_xoaDL_Click(object sender, EventArgs e)
        {

        }
        #endregion
       
        #region QL bạn đọc
        private void btn_DSBandoc_Click(object sender, EventArgs e)
        {
            FrmBanDoc frm = new FrmBanDoc();
            frm.ShowDialog();
        }

        private void btn_LaptheBandoc_Click(object sender, EventArgs e)
        {
            FrmLaptheBandoc frm = new FrmLaptheBandoc();
            frm.kt = 1; //Them
            frm.ShowDialog();
        }
        private void btn_PhatBandoc_Click_2(object sender, EventArgs e)
        {
            FrmXulyVP frm = new FrmXulyVP();
            frm.manv = maNV1;
            frm.ShowDialog();
        }
        private void btn_GiahanThe_Click(object sender, EventArgs e)
        {
            FrmGiahanThe frm = new FrmGiahanThe();
            frm.ShowDialog();
        }
        #endregion
        
        #region Quản lý ấn phẩm

        private void btn_Lapdutoan_Click(object sender, EventArgs e)
        {
            FrmDutoanMuaSach frm = new FrmDutoanMuaSach();
            frm.manv = maNV1;
            frm.ShowDialog();
        }

        private void btn_Nhap_Click(object sender, EventArgs e)
        {
            FrmQLAnpham frm = new FrmQLAnpham();
            frm.manv = maNV1;
            frm.ShowDialog();
        }

        private void btn_Inan_Click(object sender, EventArgs e)
        {
            FrmInMaDKCB frm = new FrmInMaDKCB();
            frm.ShowDialog();
        }

        private void btn_Kiemke_Click(object sender, EventArgs e)
        {
            FrmTaoPhieuKK frm = new FrmTaoPhieuKK();
            frm.manv = maNV1;
            frm.ShowDialog();
    
        }

        private void btn_Thanhly_Click(object sender, EventArgs e)
        {
            FrmThanhly frm = new FrmThanhly();
            frm.manv = maNV1;
            frm.ShowDialog();
        }
        #endregion
        
        #region lưu thông

        private void btn_DKPhongodcmo_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Chức năng chưa hoàn thiện");
        }
              
        private void btn_MuonSach_Click(object sender, EventArgs e)
        {
            FrmMuonSach frm = new FrmMuonSach();
            frm.manv = maNV1;
            frm.ShowDialog();
        }

        private void btn_Trasach_Click(object sender, EventArgs e)
        {
            FrmTraSach frm = new FrmTraSach();
            frm.ShowDialog();
        }

        private void btn_GiahanMT_Click_1(object sender, EventArgs e)
        {
            FrmGiahanMT frm = new FrmGiahanMT();
            frm.ShowDialog();
        }
        private void buttonItem3_Click(object sender, EventArgs e)
        {
            frm.frmCauHinh ch = new frm.frmCauHinh();
            ch.ShowDialog();
            cf = ch.cf;
            ch.Dispose();
            if (cf == 1)
                Application.Restart();
        }

        private void btn_GuiMail_Click(object sender, EventArgs e)
        {
            FrmGuiMail frm = new FrmGuiMail();
            frm.ShowDialog();
        }
        #endregion
        
        #region Tìm kiếm
        private void btn_TimKiemBanDoc_Click(object sender, EventArgs e)
        {
            FrmTimKiemBanDoc frm = new FrmTimKiemBanDoc();
            frm.ShowDialog();
        }

        private void btn_TimKiemAnPham_Click(object sender, EventArgs e)
        {
            FrmTimKiemAnPham frm = new FrmTimKiemAnPham();
            frm.ShowDialog();
        }

        private void btn_TimKiemMuonTra_Click(object sender, EventArgs e)
        {
            FrmTimKiemMuonTra frm = new FrmTimKiemMuonTra();
            frm.ShowDialog();
        }
        #endregion
       
        #region Menu left
        private void btn_NhapAnPham_L_Click(object sender, EventArgs e)
        {
            FrmQLAnpham frm = new FrmQLAnpham();
            frm.ShowDialog();
        }

        private void btn_LapDuToan_L_Click(object sender, EventArgs e)
        {
            FrmDutoanMuaSach frm = new FrmDutoanMuaSach();
            frm.ShowDialog();
        }

        private void btn_InMaSach_L_Click(object sender, EventArgs e)
        {
            FrmInMaDKCB frm = new FrmInMaDKCB();
            frm.ShowDialog();
        }

        private void btn_DKTheThuVien_L_Click(object sender, EventArgs e)
        {
            FrmLaptheBandoc frm = new FrmLaptheBandoc();
            frm.kt = 1; //Them
            frm.ShowDialog();
        }

        private void btn_GiaHan_L_Click(object sender, EventArgs e)
        {
            FrmGiahanThe frm = new FrmGiahanThe();
            frm.ShowDialog();
        }

        private void btn_TimKiemBanDoc_L_Click(object sender, EventArgs e)
        {
            FrmTimKiemBanDoc frm = new FrmTimKiemBanDoc();
            frm.ShowDialog();
        }
       
        private void btn_MuonSach_L_Click(object sender, EventArgs e)
        {
            FrmMuonSach frm = new FrmMuonSach();
            frm.ShowDialog();
        }

        private void btn_TraSach_L_Click(object sender, EventArgs e)
        {
            FrmTraSach frm = new FrmTraSach();
            frm.ShowDialog();
        }

        private void btn_GiaHanMT_L_Click(object sender, EventArgs e)
        {
            FrmGiahanMT frm = new FrmGiahanMT();
            frm.ShowDialog();
        }
        #endregion

        #region Button top
        private void btn_DangNhap_Top_Click(object sender, EventArgs e)
        {
            FrmLogin frm = new FrmLogin();
            frm.MyGetData = new FrmLogin.GetString(GetValue);
            frm.ShowDialog();
            string[] arrayString = conn.getArray("MaCN", "QuyenHan", "MaNV", maNV1, 1);
            int n = arrayString.Length;//Lấy số phần tử mảng
            if (lbl_status.Text != "Trạng thái chưa đăng nhập")
            {

                //Duyệt các menu chuc nang
                foreach (Control c in ribbonControl_Menu.Controls)
                {
                    if (uti.TimKiem(arrayString, n, c.Name) == true)
                        c.Enabled = true;
                }
                foreach (Control c in ribbonPanel_HeThong.Controls)
                {
                    if (uti.TimKiem(arrayString, n, c.Name) == true)
                        c.Enabled = true;
                }
                foreach (Control c in ribbonBar_NSD.Controls)
                {
                    if (uti.TimKiem(arrayString, n, c.Name) == true)
                        c.Enabled = true;
                }
                //Load menu trái
                if (ribbonPanel_AnPham.Enabled == true)
                    exp_AnPham.Enabled = true;
                if (ribbonPanel_BanDoc.Enabled == true)
                    exp_QLBanDoc.Enabled = true;
                if (ribbonPanel_LuuThong.Enabled == true)
                    exp_LuuThong.Enabled = true;
                //Load trạng thái 2 button Dang nhap va dang xuất
                btn_DangNhap.Enabled = false;
                btn_DangNhap_Top.Enabled = false;
                btn_Dangxuat.Enabled = true;
                btn_DangXuat_Top.Enabled = true;
                btn_DoiMK.Enabled = true;
                btn_DoiMK_Top.Enabled = true;
                timer1_Tick(sender, e);
            }
        }

        private void btn_DangXuat_Top_Click(object sender, EventArgs e)
        {
            loadForm(false);
            FrmMain_Load(sender, e);
            btn_DangNhap.Enabled = true;
            btn_DangNhap_Top.Enabled = true;
            //FrmLogin frm = new FrmLogin();
            //frm.ShowDialog();
        }

        private void btn_DoiMK_Top_Click(object sender, EventArgs e)
        {
            FrmDoiMK frm = new FrmDoiMK();
            frm.manv = maNV1;
            frm.ShowDialog();
        }

        private void btn_Thoat_Top_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Thống kê, báo cáo
        private void btn_reportBanDoc_Click(object sender, EventArgs e)
        {
            FrmThongKeBanDoc frm= new FrmThongKeBanDoc();
            frm.ShowDialog();
        }

        private void btn_reportSach_Click(object sender, EventArgs e)
        {
            FrmThongKeSach frm = new FrmThongKeSach();
            frm.ShowDialog();
        }

        private void btn_reportMT_Click(object sender, EventArgs e)
        {
            FrmThongKeMT frm = new FrmThongKeMT();
            frm.ShowDialog();
        }
        #endregion

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (cf != 1)
            {
                if (MessageBox.Show("Bạn có muốn thoát không?", "Hỏi",
          MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }
                else
                    this.Dispose();
            }
        }

    }
}
